/*
 *  create3Ddata.c
 *  testProject
 *
 *  Created by Christoph Seitz on 21.04.08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "create3Ddata.h"

molek * calc3d (mld * ml) {
	int i=0,j,molcount,aa;
	molek * retv;
	while ((ml+i)->m[0]!='E') {
		++i;
	}
	if (i==0) return NULL;
	molcount = i;
	retv = malloc((molcount+1)*sizeof(molek));
	i = 0;
	while ((ml+i)->m[0]!='E') {
		for (j=0;j<4;++j) {
			if ((ml+i)->bn[j]==NULL) { 
				(retv+i)->childs[j]=NULL;
			} else {
				mld * a;
				a=(ml+i)->bn[j];
				aa=(int)(a - ml);
				(retv+i)->childs[j]=retv+aa;
				if ((retv+i)->childs[j]!=(retv+i)->parrent) {
					(retv+i)->childs[j]->parrent=retv+i;
				}
			}
		}
		(retv+i)->x=0;
		(retv+i)->y=0;
		(retv+i)->z=0;
		(retv+i)->m[0]=(ml+i)->m[0];
		(retv+i)->m[1]=(ml+i)->m[1];
		++i;
	}
	(retv)->parrent=NULL;
	(retv+i)->m[0]='E';
	i=0;
	calcmol(retv);
	return retv;
}

int calcmol(molek * current) {
	int i=0,count=1;
	float a,ang,x=0,y=0,z=0,x2,y2,z2,l,aa,a1,b1,c1;;
	vec v1,v2,c[4];
	float *t;
	int *m=checkbnd(current);
	if (!(strcmp(current->m,"C\000"))) {
		ang=109.5/180.0*M_PI;
	} else 	if (!(strcmp(current->m,"C\000")) && *m==1) {
		ang=120/180.0*M_PI;
	} else 	if (!(strcmp(current->m,"C\000")) && (*m==2 || *(m+1)==1)) {
		ang=180.0/180.0*M_PI;
	} else if (!(strcmp(current->m,"N\000"))) {
		ang=106.8/180.0*M_PI;
	} else if (!(strcmp(current->m,"O\000"))) {
		ang=104.5/180.0*M_PI;
	} else if (!(strcmp(current->m,"B\000"))) {
		ang=120/180.0*M_PI;
	} else ang=0.0;
	if (current->parrent==NULL) {
		if (*m==0 && *(m+1)==0)	if (current->childs[1]!=NULL) {
			c[0][0]=-bndlngcalc(&current->m[0],&current->childs[0]->m[0],1);
			c[0][1]=0;
			c[0][2]=0;
			x=5;
			z=0;
			x2=c[0][0]*3;
			y2=c[0][1]*3;
			z2=c[0][2]*3;
			l = sqrt (c[0][0]*c[0][0]+c[0][1]*c[0][1]+c[0][2]*c[0][2]);
			//if (random()/pow(-9,10)<0.5) 
			//if (random()/pow(-9,10)<0.5) y= -sqrt((pow(x*x2,2)-x*x*cos(ang)*cos(ang)*l*l)/(l*l*cos(ang)*cos(ang)));else 
			y= sqrt((pow(x*x2,2)-x*x*cos(ang)*cos(ang)*l*l)/(l*l*cos(ang)*cos(ang)));
			//else  y = ((-1*b1)+(sqrt((b1*b1)-(4*a1*c1))))/(2*a1);
			l=bndlngcalc(&current->m[0],&current->childs[1]->m[0],1);
			a=sqrt(l*l/(x*x+y*y+z*z));
			c[1][0]=x*a;
			c[1][1]=y*a;
			c[1][2]=0;
			v1[0]=1;v1[1]=0;v1[2]=0;v2[0]=c[1][0];v2[1]=c[1][1];v2[2]=c[1][2];
			for(i=2;i<4;++i) {
				if (current->childs[i]!=NULL) {
				l=bndlngcalc(&current->m[0],&current->childs[i]->m[0],count);
				t = rotate(v1,v2,120*(i-1));
				x=*t;
				y=*(t+1);
				z=*(t+2);
				a=sqrt(l*l/(x*x+y*y+z*z));
				c[i][0]=a*x;
				c[i][1]=a*y;
				c[i][2]=a*z;
				free(t);
				}}
			for (i=0;i<4;++i) {
				if (current->childs[i]!=NULL) {
					current->childs[i]->x=c[i][0];
					current->childs[i]->y=c[i][1];
					current->childs[i]->z=c[i][2];
					calcmol(current->childs[i]);
				}
			}
		}
		if (*m==1) {
			c[0][0]=-bndlngcalc(&current->m[0],&current->childs[*(m+2)-1]->m[0],2);
			c[0][1]=0;
			c[0][2]=0;
			if (current->childs[2]!=NULL) {
			x=5;
			z=0;
			x2=c[0][0]*3;
			y2=c[0][1]*3;
			z2=c[0][2]*3;
			l = sqrt (c[0][0]*c[0][0]+c[0][1]*c[0][1]+c[0][2]*c[0][2]);
			y= sqrt((pow(x*x2,2)-x*x*cos(ang)*cos(ang)*l*l)/(l*l*cos(ang)*cos(ang)));
			if (*(m+2)==1) l=bndlngcalc(&current->m[0],&current->childs[2]->m[0],1);
			if (*(m+2)==2) l=bndlngcalc(&current->m[0],&current->childs[0]->m[0],1);
			if (*(m+2)==3) l=bndlngcalc(&current->m[0],&current->childs[0]->m[0],1);
			a=sqrt(l*l/(x*x+y*y+z*z));
			c[1][0]=x*a;
			c[1][1]=y*a;
			c[1][2]=0;
			v1[0]=1;v1[1]=0;v1[2]=0;v2[0]=c[1][0];v2[1]=c[1][1];v2[2]=c[1][2];
			if (*(m+2)==1) l=bndlngcalc(&current->m[0],&current->childs[3]->m[0],count);
			if (*(m+2)==2) l=bndlngcalc(&current->m[0],&current->childs[3]->m[0],1);
			if (*(m+2)==3) l=bndlngcalc(&current->m[0],&current->childs[1]->m[0],1);
			t = rotate(v1,v2,180.0);
			x=*t;
			y=*(t+1);
			z=*(t+2);
			a=sqrt(l*l/(x*x+y*y+z*z));
			c[2][0]=x*a;
			c[2][1]=y*a;
			c[2][2]=z*a;
			free(t);
			}
			int f,g,h;
			if (*(m+2)==1) {
				f=0;g=2;h=3;
			}
			if (*(m+2)==2) {
				f=1;g=0;h=3;
			}
			if (*(m+2)==3) {
				f=2;g=0;h=1;
			}
			if(current->childs[f]!=NULL) {current->childs[f]->x=c[0][0];current->childs[f]->y=c[0][1];current->childs[f]->z=c[0][2];calcmol(current->childs[f]);}					
			if(current->childs[g]!=NULL) {current->childs[g]->x=c[1][0];current->childs[g]->y=c[1][1];current->childs[g]->z=c[1][2];calcmol(current->childs[g]);}
			if(current->childs[h]!=NULL) {current->childs[h]->x=c[2][0];current->childs[h]->y=c[2][1];current->childs[h]->z=c[2][2];calcmol(current->childs[h]);}
		} 
		if (*(m+1)==1 && current->childs[3]!=NULL) {
			if (*(m+3)==1) {
				c[0][0]= -bndlngcalc(&current->m[0], &current->childs[0]->m[0], 3);
				c[0][1]=0;
				c[0][2]=0;
				c[1][0]= bndlngcalc(&current->m[0], &current->childs[3]->m[0], 1);
				c[1][1]=0;
				c[1][2]=0;
				if(current->childs[0]!=NULL) {current->childs[0]->x=c[0][0];current->childs[0]->y=c[0][1];current->childs[0]->z=c[0][2];calcmol(current->childs[0]);}
				if(current->childs[3]!=NULL) {current->childs[3]->x=c[1][0];current->childs[3]->y=c[1][1];current->childs[3]->z=c[1][2];calcmol(current->childs[3]);}
			}
			if (*(m+3)==2) {
				c[0][0]= -bndlngcalc(&current->m[0], &current->childs[0]->m[0], 1);
				c[0][1]=0;
				c[0][2]=0;
				c[1][0]= bndlngcalc(&current->m[0], &current->childs[3]->m[0], 3);
				c[1][1]=0;
				c[1][2]=0;
				if(current->childs[3]!=NULL) {current->childs[3]->x=c[0][0];current->childs[3]->y=c[0][1];current->childs[3]->z=c[0][2];calcmol(current->childs[3]);}
				if(current->childs[0]!=NULL) {current->childs[0]->x=c[1][0];current->childs[0]->y=c[1][1];current->childs[0]->z=c[1][2];calcmol(current->childs[0]);}

			}
		}
		if (*(m)==2 && current->childs[2]!=NULL) {
			c[0][0]=-bndlngcalc(&current->childs[0]->m[0], &current->m[0], 2);
			c[0][1]=0;
			c[0][2]=0;
			c[1][0]=bndlngcalc(&current->childs[2]->m[0], &current->m[0], 2);
			c[1][1]=0;
			c[1][2]=0;
			if(current->childs[0]!=NULL && current->childs[0]!=current->parrent) {current->childs[0]->x=c[0][0];current->childs[0]->x=c[0][0];current->childs[0]->z=c[0][2];calcmol(current->childs[0]);}
			if(current->childs[2]!=NULL && current->childs[2]!=current->parrent) {current->childs[2]->x=c[1][0];current->childs[2]->y=c[1][1];current->childs[2]->z=c[1][2];calcmol(current->childs[2]);}
		}
	} else {
		if (*(m)==1 && current->childs[2]!=NULL) {
				vec n,e,p2;
				e[0]=current->x-current->parrent->x;
				e[1]=current->y-current->parrent->y;
				e[2]=current->z-current->parrent->z;
				n[0]=current->parrent->x-current->x;
				n[1]=current->parrent->y-current->y;
				n[2]=current->parrent->z-current->z;
				aa=sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
				n[0]=n[0]/aa;
				n[1]=n[1]/aa;
				n[2]=n[2]/aa;
				x2 =  e[0];
				y2 =  e[1];
				z2 =  e[2];
				x= e[0]*5;
				y= e[1]*5;
				z=0;
				l=sqrt(x2*x2+y2*y2+z2*z2);
				if (e[2]!=0) {
					a1=(z2*z2)-(cos(ang)*cos(ang)*l*l);
					b1=((2*x*x2*z2)+(2*y*y2*z2));
					c1=(2*x*x2*y*y2)+(x*x*x2*x2)+(y*y*y2*y2);
					//if (random()/pow(-9,10)<0.5) 
					//else
					if(z2<=0) z=((-1*b1)-(sqrt((b1*b1)-(4*a1*c1))))/(2*a1);
					if(z2>=0)z= ((-1*b1)+(sqrt((b1*b1)-(4*a1*c1))))/(2*a1); 
				} else if(e[1]!=0) {
					a1=y2-(cos(ang)*cos(ang)*l*l);
					b1=2*x*x2*y2;
					c1=(x*x*x2*x2)-(x*x*cos(ang)*cos(ang)*l*l);
					//if (random()/pow(-9,10)<0.5) 
					y = ((-1*b1)-(sqrt((b1*b1)-(4*a1*c1))))/(2*a1);
					//else  y = ((-1*b1)+(sqrt((b1*b1)-(4*a1*c1))))/(2*a1);
				} else {
					//sranddev();
					//if (random()/pow(-9,10)<0.5) 
					y= -sqrt((pow(x*x2,2)-x*x*cos(ang)*cos(ang)*l*l)/(l*l*cos(ang)*cos(ang)));
					//else y=sqrt((pow(x*x2,2)-x*x*cos(ang)*cos(ang)*l*l)/(l*l*cos(ang)*cos(ang)));
				}
				p2[0]=x;c[0][0]=p2[0];
				p2[1]=y;c[0][1]=p2[1];
				p2[2]=z;c[0][2]=p2[2];
				t = rotate(n, p2, 180.0);
				x=*(t+0);
				y=*(t+1);
				z=*(t+2);
				c[1][0]=x;
				c[1][1]=y;
				c[1][2]=z;
				n[0]=-1*p2[0];
				n[1]=-1*p2[1];
				n[2]=-1*p2[2];
				aa=sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
				n[0]=n[0]/aa;
				n[1]=n[1]/aa;
				n[2]=n[2]/aa;
				t = rotate(n, e, 180.0);
				x=*(t+0);
				y=*(t+1);
				z=*(t+2);
				c[2][0]=x;
				c[2][1]=y;
				c[2][2]=z;
				free(t);
			if ( (*(m+2)==1 && current->parrent==current->childs[0]) || (*(m+2)==2 && current->parrent==current->childs[1]) || (*(m+2)==3 && current->parrent==current->childs[2])) {
				int g,h;
				if (*(m+2)==1) {g=2;h=3;}
				if (*(m+2)==2) {g=0;h=3;}
				if (*(m+2)==3) {g=0;h=1;}
				l=bndlngcalc(&current->m[0], &current->childs[g]->m[0], 1);
				a=sqrt(l*l/(c[0][0]*c[0][0]+c[0][1]*c[0][1]+c[0][2]*c[0][2]));
				c[0][0]=c[0][0]*a+current->x;
				c[0][1]=c[0][1]*a+current->y;
				c[0][2]=c[0][2]*a+current->z;
				l=bndlngcalc(&current->m[0], &current->childs[h]->m[0], 1);
				a=sqrt(l*l/(c[1][0]*c[1][0]+c[1][1]*c[1][1]+c[1][2]*c[1][2]));
				c[1][0]=c[1][0]*a+current->x;
				c[1][1]=c[1][1]*a+current->y;
				c[1][2]=c[1][2]*a+current->z;
				if(current->childs[g]!=NULL) {current->childs[g]->x=c[0][0];current->childs[g]->y=c[0][1];current->childs[g]->z=c[0][2];calcmol(current->childs[g]);}
				if(current->childs[h]!=NULL) {current->childs[h]->x=c[1][0];current->childs[h]->y=c[1][1];current->childs[h]->z=c[1][2];calcmol(current->childs[h]);}
			}
			if ( (*(m+2)==1 && current->parrent!=current->childs[0]) || (*(m+2)==2 && current->parrent!=current->childs[1]) || (*(m+2)==3 && current->parrent!=current->childs[2])) {
				int g,h;
				if (*(m+2)==1) {g=2;h=3;}
				if (*(m+2)==2) {g=0;h=3;}
				if (*(m+2)==3) {g=0;h=1;}
				i = *(m+2)-1;
				l=bndlngcalc(&current->m[0], &current->childs[i]->m[0], 2);
				a=sqrt(l*l/(c[0][0]*c[0][0]+c[0][1]*c[0][1]+c[0][2]*c[0][2]));
				c[0][0]=c[0][0]*a+current->x;
				c[0][1]=c[0][1]*a+current->y;
				c[0][2]=c[0][2]*a+current->z;
				l=bndlngcalc(&current->m[0], &current->childs[g]->m[0], 1);
				a=sqrt(l*l/(c[1][0]*c[1][0]+c[1][1]*c[1][1]+c[1][2]*c[1][2]));
				c[1][0]=c[1][0]*a+current->x;
				c[1][1]=c[1][1]*a+current->y;
				c[1][2]=c[1][2]*a+current->z;
				l=bndlngcalc(&current->m[0], &current->childs[h]->m[0], 1);
				a=sqrt(l*l/(c[1][0]*c[1][0]+c[1][1]*c[1][1]+c[1][2]*c[1][2]));
				c[2][0]=c[2][0]*a+current->x;
				c[2][1]=c[2][1]*a+current->y;
				c[2][2]=c[2][2]*a+current->z;
				if(current->childs[g]!=NULL && current->childs[g]!=current->parrent ) {current->childs[g]->x=c[1][0];current->childs[g]->y=c[1][1];current->childs[g]->z=c[1][2];calcmol(current->childs[g]);}
				if(current->childs[i]!=NULL) {current->childs[i]->x=c[0][0];current->childs[i]->y=c[0][1];current->childs[i]->z=c[0][2];calcmol(current->childs[i]);}
				if(current->childs[h]!=NULL && current->childs[h]!=current->parrent) {current->childs[h]->x=c[1][0];current->childs[h]->y=c[1][1];current->childs[h]->z=c[1][2];calcmol(current->childs[h]);}
			}
		}
		if (*(m+1)!=0 && current->childs[3]!=NULL) {
			vec n;
			int pa;
			n[0]=current->x-current->parrent->x;
			n[1]=current->y-current->parrent->y;
			n[2]=current->z-current->parrent->z;
			if (*(m+2)=1) l=bndlngcalc(&current->m[0], &current->childs[3]->m[0], 1);
			else l=bndlngcalc(&current->m[0], &current->childs[3]->m[0], 3);
			a=sqrt(l*l/(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]));
			if (current->childs[0]==current->parrent) pa=3;
			else pa=0;
			current->childs[pa]->x=n[0]*a+current->x;
			current->childs[pa]->y=n[1]*a+current->y;
			current->childs[pa]->z=n[2]*a+current->z;
			if(current->childs[pa]!=NULL) calcmol(current->childs[pa]);
		}
		if (*m==0 && *(m+1)==0 && current->childs[1]!=NULL) {
			vec n,e,p2;
			e[0]=current->x-current->parrent->x;
			e[1]=current->y-current->parrent->y;
			e[2]=current->z-current->parrent->z;
			n[0]=current->parrent->x-current->x;
			n[1]=current->parrent->y-current->y;
			n[2]=current->parrent->z-current->z;
			aa=sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
			n[0]=n[0]/aa;
			n[1]=n[1]/aa;
			n[2]=n[2]/aa;
			x2 =  e[0];
			y2 =  e[1];
			z2 =  e[2];
			x= e[0]*5;
			y= e[1]*5;
			z=0;
			if (current->childs[1]!=NULL) {
				l=sqrt(x2*x2+y2*y2+z2*z2);
				if (e[2]!=0) {
					a1=(z2*z2)-(cos(ang)*cos(ang)*l*l);
					b1=((2*x*x2*z2)+(2*y*y2*z2));
					c1=(2*x*x2*y*y2)+(x*x*x2*x2)+(y*y*y2*y2);
					//if (random()/pow(-9,10)<0.5) 
					//else
					if(z2<=0) z=((-1*b1)-(sqrt((b1*b1)-(4*a1*c1))))/(2*a1);
					if(z2>=0)z= ((-1*b1)+(sqrt((b1*b1)-(4*a1*c1))))/(2*a1); 
				} else if(e[1]!=0) {
					a1=y2-(cos(ang)*cos(ang)*l*l);
					b1=2*x*x2*y2;
					c1=(x*x*x2*x2)-(x*x*cos(ang)*cos(ang)*l*l);
					//if (random()/pow(-9,10)<0.5) 
						y = ((-1*b1)-(sqrt((b1*b1)-(4*a1*c1))))/(2*a1);
					//else  y = ((-1*b1)+(sqrt((b1*b1)-(4*a1*c1))))/(2*a1);
				} else {
					//sranddev();
					//if (random()/pow(-9,10)<0.5) 
						y= -sqrt((pow(x*x2,2)-x*x*cos(ang)*cos(ang)*l*l)/(l*l*cos(ang)*cos(ang)));
					//else y=sqrt((pow(x*x2,2)-x*x*cos(ang)*cos(ang)*l*l)/(l*l*cos(ang)*cos(ang)));
				}
				l=bndlngcalc(&current->m[0], &current->childs[1]->m[0], 1);
				a=sqrt(l*l/(x*x+y*y+z*z));
				p2[0]=x*a;
				p2[1]=y*a;
				p2[2]=z*a;
				for (i=0;i<3;++i) {
					if (current->childs[i+1]!=NULL) {
						t = rotate(n, p2, 120*i);
						x=*(t+0);
						y=*(t+1);
						z=*(t+2);
						l=bndlngcalc(&current->m[0], &current->childs[i+1]->m[0], 1);
						a=sqrt(l*l/(x*x+y*y+z*z));
						c[i][0]=current->x+x*a;
						c[i][1]=current->y+y*a;
						c[i][2]=current->z+z*a;
						free(t);
					}
				}
				int u=0;
				for (i=1;i<4;++i) {
					if (current->childs[i]!=NULL && current->childs[i]!=current->parrent) {
						current->childs[i]->x=c[u][0];
						current->childs[i]->y=c[u][1];
						current->childs[i]->z=c[u][2];
						++u;
						if (current->childs[i]!=current->parrent) calcmol(current->childs[i]);
					}
				}
			}
		}
	}
	free(m);
	return 1;
}
int * checkbnd(molek * current) {
	int i;
	int * retv = malloc(4*sizeof(int));
	for (i=0;i<4;++i) *(retv+i)=0;
	if (current->childs[0]==current->childs[1] && current->childs[1]!=current->childs[2] && (current->childs[2]!=current->childs[3] || (current->childs[3]==NULL && current->childs[2]==NULL))) {*(retv)=1;*(retv+2)=1;} else
	if (current->childs[0]!=current->childs[1] && current->childs[1]==current->childs[2] && current->childs[2]!=current->childs[3]) {*(retv)=1;*(retv+2)=2;} else
	if (current->childs[0]!=current->childs[1] && current->childs[1]!=current->childs[2] && current->childs[2]==current->childs[3] && current->childs[3]!=NULL && current->childs[2]!=NULL) {*(retv)=1;*(retv+2)=3;} else
	if (current->childs[0]==current->childs[1] && current->childs[1]!=current->childs[2] && current->childs[2]==current->childs[3] && current->childs[3]!=NULL && current->childs[2]!=NULL) {*(retv)=2;*(retv+2)=1;} else
	if (current->childs[0]==current->childs[1] && current->childs[1]==current->childs[2] && current->childs[2]!=current->childs[3]) {*(retv+1)=1;*(retv+3)=1;} else
	if (current->childs[0]!=current->childs[1] && current->childs[1]==current->childs[2] && current->childs[2]==current->childs[3] && current->childs[3]!=NULL && current->childs[2]!=NULL) {*(retv+1)=2;*(retv+3)=2;} else {
		return retv;
	}
	return retv;
}

float * molcolor(char * mol) {
	float * retv=malloc(3*sizeof(float));
	int i;
	for (i=0;i<20;++i) {
		if(!(strcmp(mol,mcol[i].m))) {
			retv[0] = mcol[i].r;
			retv[1] = mcol[i].g;
			retv[2] = mcol[i].b;
			return retv;
		}
	}
	for (i=0; i<3;++i) retv[i] = 0.1;
	return retv;
}

float * rotate(vec v,vec v1,float ang) {
	float m[3][3];
	int i;
	vec v2;
	float *retv;
	float a=ang/180.0*M_PI;
	m[0][0] = cos(a)+v[0]*v[0]*(1-cos(a));
	m[0][1] = v[1]*v[0]*(1-cos(a))+v[2]*sin(a) ;
	m[0][2] = v[2]*v[0]*(1-cos(a))-v[1]*sin(a) ;
	m[1][0] = v[0]*v[1]*(1-cos(a))-v[2]*sin(a);
	m[1][1] = cos(a)+v[1]*v[1]*(1-cos(a)) ;
	m[1][2] = v[2]*v[1]*(1-cos(a))+v[0]*sin(a) ;
	m[2][0] = v[0]*v[2]*(1-cos(a))+v[0]*sin(a) ;
	m[2][1] = v[1]*v[2]*(1-cos(a))-v[1]*sin(a);
	m[2][2] = cos(a)+v[2]*v[2]*(1-cos(a));
	for (i=0;i<3;++i) {
		v2[i] = *v1*m[0][i]+*(v1+1)*m[1][i]+*(v1+2)*m[2][i];
	}
	retv = malloc(3*sizeof(float));
	for (i=0;i<3;++i) {
		*retv = v2[i];
		++retv;
	}
	retv=retv-3;
	return retv;
}
float  calcbndr(char * mol) {
	float  retv;
	int i;
	for (i=0;i<20;++i) {
		if(!(strncmp(mol,bndr[i].m,2))) {
			retv=bndr[i].l;
			return retv;
		}
	}
	return 100.0;
}

float bndlngcalc(char * c1,char * c2,int count) {
	float retv;
	int i;
	for (i=0;i<23;++i) {
		if (*(c1+1)!='\0') {
			if (*(c2+1)!='\0') {
				if ((*c1==bndl[i].m[0] && atoi(&bndl[i].m[2])==count && *c2==bndl[i].m[3] && *(c1+1)==bndl[i].m[1] && *(c2+1)==bndl[i].m[4]) || 
					(*c2==bndl[i].m[0] && atoi(&bndl[i].m[2])==count && *c1==bndl[i].m[3] && *(c2+1)==bndl[i].m[1] && *(c1+1)==bndl[i].m[4])) {
					retv = bndl[i].l;
					return retv;
				}
			} else {
				if ((*c1==bndl[i].m[0] && atoi(&bndl[i].m[2])==count && *c2==bndl[i].m[3] && *(c1+1)==bndl[i].m[1]  ) || 
					(*c2==bndl[i].m[0] && atoi(&bndl[i].m[1])==count && *c1==bndl[i].m[2] && *(c1+1)==bndl[i].m[3]  )) {
					retv = bndl[i].l;
					return retv;
				}
			}
		} else {
			if (*(c2+1)!='\0') {
				if ((*c1==bndl[i].m[0] && atoi(&bndl[i].m[1])==count && *c2==bndl[i].m[2] && *(c2+1)==bndl[i].m[3]  ) ||
					(*c2==bndl[i].m[0] && atoi(&bndl[i].m[2])==count && *c1==bndl[i].m[3] && *(c2+1)==bndl[i].m[1]  )) {
					retv = bndl[i].l;
					return retv;
				}
			} else {
				if((*c1==bndl[i].m[0] && atoi(&bndl[i].m[1])==count && *c2==bndl[i].m[2]) || (*c2==bndl[i].m[0] && atoi(&bndl[i].m[1])==count && *c1==bndl[i].m[2] )) {
					retv = bndl[i].l;
					return retv;
				}
			}
		}
	}
	retv = 5;
	return retv;
}
int checkspace(vec v1,vec v2,molek * current) {
	molek * c[10];
	int ang,i;
	float sl,s=0,a=0;
	vec r = {*v1,*(v1+1),*(v1+2)};
	if (current->parrent!=NULL) c[0]=current->parrent;
	if (current->parrent->childs[0]!=NULL) c[1]=current->parrent->childs[0];
	if (current->parrent->childs[1]!=NULL) c[2]=current->parrent->childs[1];
	if (current->parrent->childs[2]!=NULL) c[3]=current->parrent->childs[2];
	if (current->parrent->childs[3]!=NULL) c[4]=current->parrent->childs[3];
	if (current->parrent->parrent!=NULL) c[5]=current->parrent->parrent;
	if (current->parrent->parrent->childs[0]!=NULL) c[6]=current->parrent->parrent->childs[0];
	if (current->parrent->parrent->childs[1]!=NULL) c[7]=current->parrent->parrent->childs[1];
	if (current->parrent->parrent->childs[2]!=NULL) c[8]=current->parrent->parrent->childs[2];
	if (current->parrent->parrent->childs[3]!=NULL) c[9]=current->parrent->parrent->childs[3];
	for (ang=0;ang<360;ang=ang+5) {
		for (i=0;i<10;++i) {
			if (c[i]!=NULL) {
				a = sqrt(pow(current->x-c[i]->x,2)+pow(current->y-c[i]->y,2)+pow(current->z-c[i]->z,2));
			}
		}
	}
}
